package org.casper.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import org.casper.bean.Board;
import org.casper.util.DBUtil;
import org.casper.util.DateUtil;

public class BoardDAO {

	/**
	 * 增加一个版块
	 * description:
	 * 2015年10月22日 下午4:28:58
	 * casper
	 * int
	 * @param newBoard
	 * @return
	 */
	public int addBoard(Board newBoard) {
		int resultCode = -1;
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		try {
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement("insert into t_board(boardname,boarddesc,pid,addtime) values(?,?,?,?)");
			pstmt.setString(1, newBoard.getBoardName());
			pstmt.setString(2, newBoard.getBoardDesc());
			pstmt.setInt(3, newBoard.getPbid());
			pstmt.setString(4, newBoard.getAddTime());
			resultCode = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(conn, pstmt);
		}
		return resultCode;
	}
	
	/**
	 * 查选全部的版块
	 * description:
	 * 2015年10月22日 下午4:45:46
	 * casper
	 * ArrayList<Board>
	 * @return
	 */
	public ArrayList<Board> selectAllBoards() {
		ArrayList<Board> boards = new ArrayList<Board>();
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		conn = DBUtil.getConn();
		try {
			pstmt = conn.prepareStatement("select * from t_board order by addtime desc");
			rs = pstmt.executeQuery();
			Board board = null;
			while(rs.next()){
				board = new Board(rs.getInt("id"), rs.getString("boardname"),rs.getString("boarddesc"), rs.getInt("pid"),rs.getString("addtime"));
				boards.add(board);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(conn, pstmt, rs);
		}
		return boards;
	}
	
	
	/**
	 * 测试
	 * description:
	 * 2015年10月22日 下午4:46:12
	 * casper
	 * void
	 * @param args
	 */
	public static void main(String[] args) {
		BoardDAO boardDao = new BoardDAO();
		//System.out.println(boardDao.addBoard(new Board("HTML", "网页标签语言", 0, DateUtil.toDateStr(new Date()))));
		for(Board b:boardDao.selectAllBoards()){
			System.out.println(b);
		}
		System.out.println(boardDao.checkBoardNameOnly("CSS"));
	}
	
	/**
	 * description:检查版块名字的唯一
	 * 2015年10月22日 下午5:22:09
	 * casper
	 * boolean
	 * @param boardName
	 * @return
	 */
	public boolean checkBoardNameOnly(String boardName) {
		boolean isOnly= false;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement("select boardname from t_board where boardname=?");
			pstmt.setString(1, boardName);
			rs = pstmt.executeQuery();
			while(rs.next()){
				if(rs.getString("boardname").equals(boardName)){
					isOnly = true;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(conn, pstmt, rs);
		}
		return isOnly;
	}
}
